package com.wuhongyu.controller;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wuhongyu.common.MyCommon;
import com.wuhongyu.entity.Brand;
import com.wuhongyu.service.impl.BrandServiceImpl;
import com.wuhongyu.util.MyUtil;

@Controller
public class BrandController {

	@Autowired
	private BrandServiceImpl brandServiceImpl;
	
	/**
	 * 品牌的列表
	 * @param model
	 * @param currentPage 分页
	 * @return
	 */
	@RequestMapping("/brandList")
	public String brandList(Model model,@Param("currentPage")Integer currentPage) {
		PageHelper.startPage(MyUtil.getCurrentPage(currentPage), MyCommon.PAGESIZE);
		List<Brand> brandList = brandServiceImpl.selectAll();
		PageInfo<Brand> page = new PageInfo<Brand>(brandList);
		model.addAttribute("brandList", brandList);
		model.addAttribute("page", page);
		return "admin/brand/brandList";
	}
	
	/**
	 * 修改(插入)之前的查询操作
	 * @param model
	 * @param currentPage 分页
	 * @param id 判断是修改还是插入
	 * @return
	 */
	@RequestMapping("queryBeforeUpdateOrInsertBrand")
	public String queryBeforeUpdateOrInsertBrand(Model model, @Param("currentPage")Integer currentPage,
			@Param("id")Integer id) {
		if(id!=null && id!=0) {
			Brand brand = brandServiceImpl.queryById(id);
			model.addAttribute("brand", brand);
			model.addAttribute("currentPage", currentPage);
		}
		return "admin/brand/updateOrInsertBrand";
	}
	
	/**
	 * 修改(插入)
	 * @param model
	 * @param currentPage 分页
	 * @param brand 前端传来的修改(插入)的数据
	 * @return
	 */
	@RequestMapping("/updateOrInsertBrand")
	public String updateOrInsertBrand(Model model,@Param("currentPage")Integer currentPage,
			@Param("Brand")Brand brand) {
		brandServiceImpl.updateOrInsert(brand);
		return this.brandList(model, currentPage);
	}
	
	/**
	 * 删除单行
	 * @param model
	 * @param id
	 * @param currentPage 分页
	 * @return
	 */
	@RequestMapping("/deleteBrand")
	public String deleteBrand(Model model,@Param("id")Integer id,@Param("currentPage")Integer currentPage) {
		brandServiceImpl.deleteById(id);
		return this.brandList(model, currentPage);
	}
	
	/**
	 * 删除多行
	 * @param model
	 * @param checkId 多行id
	 * @param currentPage 分页
	 * @return
	 */
	@RequestMapping("/deleteBachBrand")
	public String deleteBachBrand(Model model,@Param("checkId")Integer[] checkId,@Param("currentPage")Integer currentPage) {
		brandServiceImpl.deleteBath(checkId);
		return this.brandList(model, currentPage);
	}
}
